package de.pge.moviemeta;

import com.google.common.base.Predicate;
import java.util.List;

import com.omertron.themoviedbapi.TheMovieDbApi;
import com.omertron.themoviedbapi.model.MovieDb;
import java.io.File;
import java.util.Collections;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class Main {

    private static String normalizeString(String name) {
        //System.out.println("normalizing: [" + name + "]");

        // replaces dots with spaces
        name = name.replace('.', ' ').trim();
        //System.out.println("removed special chars: [" + name + "]");

        
        name = name.replaceAll("(.*)\\(\\d{4}\\)$", "$1");
        //System.out.println("found and removed year pattern: [" + name + "]");
        return name;
    }

    private static String getDisplayMovieTitle(MovieDb movieDb) {
        return "found possible match: " + movieDb.getOriginalTitle() + "(" + movieDb.getReleaseDate() + ")" +  " | " + movieDb.getTitle();
    }

    enum FileTypePredicate implements Predicate<File> {

        STANDARD {
            @Override
            public boolean apply(final File input) {
                return true;
            }
        },
        MOVIES {
            @Override
            public boolean apply(final File input) {
                if (input.getName().endsWith(".mkv")
                        || input.getName().endsWith(".avi")) {
                    return true;
                }
                return false;
            }
        }
    }
    private static String apiKey = "08cf65890b952140929156ebe34d722d";
    private static String startPath = "X:\\_movies\\0-9\\";
    private static DirectoryScanner dirScanner;
    private static Predicate<File> predicateDirectory;

    public static void main(String[] args) throws Exception {

        Iterable<File> directories = DirectoryScanner.forDirectory(startPath)
                .maxScanDepth(0)
                .restrictFiles(FileTypePredicate.STANDARD)
                .restrictDirectories(FileTypePredicate.STANDARD)
                .getDirectories();
        
        for (File directory : directories) {
            System.out.println("working with: " + directory);
            String guessedMovieName = normalizeString(directory.getName());
            
            System.out.println("guessing movie name: " + guessedMovieName);
            //search movies for name
            List<MovieDb> movies = getMoviesForName(guessedMovieName);
            for (MovieDb movieDb : movies) {
                System.out.println(getDisplayMovieTitle(movieDb));
            }
            
            Iterable<File> files = DirectoryScanner.forDirectory(directory)
                .maxScanDepth(0)
                .restrictFiles(FileTypePredicate.MOVIES)
                .restrictDirectories(FileTypePredicate.STANDARD)
                .getFiles();
            for (File file : files) {
                System.out.println("found movie file: " + file);
            }
            
        }
    }
    
    private static List<MovieDb> getMoviesForName(String name) {
        List<MovieDb> movies = Collections.emptyList();
        try {
        TheMovieDbApi theMovieDbApi = new TheMovieDbApi(apiKey);
        movies = theMovieDbApi.searchMovie(name, 0,
                "de", false, 0);
        } catch (Exception e) {
            System.out.println("error while getting movie information from moviedb, returning empty list");
            e.printStackTrace();
        }
        return movies;
    }
    
}
